﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LdCms.Plugins.Map.Amap
{
    using LdCms.Common.Logs;
    using LdCms.Common.Net;
    using LdCms.Common.Security;
    using LdCms.Common.Web;
    using LdCms.Common.Json;
    using LdCms.Common.Web.Http;
    using LdCms.Plugins.Map.Amap.Common;
    using LdCms.Plugins.Map.Amap.Models.GeoFence;
    
    /// <summary>
    /// 地理围栏接口
    ///     注：已完成
    /// 
    /// 
    /// </summary>
    public partial class GeoFenceHelper
    {
        public string Key { get; set; }
        public string PrivateKey { get; set; }
        public GeoFenceHelper(string key,string privateKey)
        {
            this.Key = key;
            this.PrivateKey = PrivateKey;
        }

        private string randomString = GeneralCodeHelper.GetRandomInt(8);

        private string metaUrl = "https://restapi.amap.com/v4/geofence/meta";
        private string statusUrl = "https://restapi.amap.com/v4/geofence/status";
        
        private string CreateUrl()
        {
            try
            {
                string url = metaUrl;
                return string.Format("{0}?key={1}", url, Key);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        private string QueryUrl(GeoFenceQuery query)
        {
            try
            {
                query.key = Key;
                string url = metaUrl;
                var sdParams = Utilities.EntityToSortedDictionary(query);
                string urlParams = Utilities.SortedDictionaryToUrlParams(sdParams);
                return string.Format("{0}?{1}", url, urlParams);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        private string UpdateUrl(string gid)
        {
            try
            {
                string url = metaUrl;
                return string.Format("{0}?key={1}&gid={2}", url, Key, gid);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        private string EnableUrl(string gid)
        {
            try
            {
                string url = metaUrl;
                return string.Format("{0}?key={1}&gid={2}", url, Key, gid);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        private string DeleteUrl(string gid)
        {
            try
            {
                string url = metaUrl;
                return string.Format("{0}?key={1}&gid={2}", url, Key, gid);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        private string StatusUrl(GeoFenceStatus status)
        {
            try
            {
                status.key = Key;
                string url = statusUrl;
                string path = new Uri(url).AbsolutePath;
                var sdParams = Utilities.EntityToSortedDictionary(status);
                string sign = Utilities.GetSign(sdParams, path, PrivateKey);
                string urlParams = Utilities.SortedDictionaryToUrlParams(sdParams);
                return string.Format("{0}?{1}&sig={2}", url, urlParams, sign);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }


        public string CreateGeoFence(GeoFenceCreate entity)
        {
            try
            {
                string url = CreateUrl();
                string data = entity.ToJson();
                string result = HttpHelper.PostToUrl(url, HttpRequestEnum.ContentType.Json, data);
                LogsManager.LogPath = string.Empty;
                LogsManager.logFielPrefix = string.Empty;
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-高德地图WebServiceAPI--开结--", randomString));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-IP：{1}", randomString, Net.Ip));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-地址：{1}", randomString, url));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-参数：{1}", randomString, data.MiniClear()));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-结果：{1}", randomString, result.MiniClear()));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-高德地图WebServiceAPI--结束--", randomString));
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public GeoFenceCreateResult CreateGeoFencePro(GeoFenceCreate entity)
        {
            try
            {
                string result = CreateGeoFence(entity);
                return result.ToObject<GeoFenceCreateResult>();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public string GetGeoFence()
        {
            return GetGeoFence(new GeoFenceQuery() { key = Key });
        }
        public string GetGeoFence(GeoFenceQuery query)
        {
            try
            {
                string url = QueryUrl(query);
                string result = HttpHelper.GetToUrl(url);
                LogsManager.LogPath = string.Empty;
                LogsManager.logFielPrefix = string.Empty;
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-高德地图WebServiceAPI--开结--", randomString));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-IP：{1}", randomString, Net.Ip));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-地址：{1}", randomString, url));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-结果：{1}", randomString, result.MiniClear()));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-高德地图WebServiceAPI--结束--", randomString));
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public GeoFenceQueryResult GetGeoFencePro()
        {
            try
            {
                string result = GetGeoFence();
                return result.ToObject<GeoFenceQueryResult>();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public GeoFenceQueryResult GetGeoFencePro(GeoFenceQuery query)
        {
            try
            {
                string result = GetGeoFence(query);
                return result.ToObject<GeoFenceQueryResult>();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public string UpdateGeoFence(string gid, GeoFenceUpdate entity)
        {
            try
            {
                string url = UpdateUrl(gid);
                string data = entity.ToJson();
                string result = HttpHelper.PatchToUrl(url, HttpRequestEnum.ContentType.Json, data);
                LogsManager.LogPath = string.Empty;
                LogsManager.logFielPrefix = string.Empty;
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-高德地图WebServiceAPI--开结--", randomString));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-IP：{1}", randomString, Net.Ip));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-地址：{1}", randomString, url));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-参数：{1}", randomString, data.MiniClear()));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-结果：{1}", randomString, result.MiniClear()));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-高德地图WebServiceAPI--结束--", randomString));
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public GeoFenceUpdateResult UpdateGeoFencePro(string gid, GeoFenceUpdate entity)
        {
            try
            {
                string result = UpdateGeoFence(gid, entity);
                return result.ToObject<GeoFenceUpdateResult>();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public string UpdateGeoFenceEnable(string gid, bool enable)
        {
            try
            {
                string url = EnableUrl(gid);
                string data = new GeoFenceUpdateEnable() { enable = enable }.ToJson();
                string result = HttpHelper.PatchToUrl(url, HttpRequestEnum.ContentType.Json, data);
                LogsManager.LogPath = string.Empty;
                LogsManager.logFielPrefix = string.Empty;
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-高德地图WebServiceAPI--开结--", randomString));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-IP：{1}", randomString, Net.Ip));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-地址：{1}", randomString, url));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-参数：{1}", randomString, data.MiniClear()));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-结果：{1}", randomString, result.MiniClear()));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-高德地图WebServiceAPI--结束--", randomString));
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public GeoFenceUpdateEnableResult UpdateGeoFenceEnablePro(string gid, bool enable)
        {
            try
            {
                string result = UpdateGeoFenceEnable(gid, enable);
                return result.ToObject<GeoFenceUpdateEnableResult>();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public string DeleteGeoFence(string gid)
        {
            try
            {
                string url = DeleteUrl(gid);
                string result = HttpHelper.DeleteToUrl(url);
                LogsManager.LogPath = string.Empty;
                LogsManager.logFielPrefix = string.Empty;
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-高德地图WebServiceAPI--开结--", randomString));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-IP：{1}", randomString, Net.Ip));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-地址：{1}", randomString, url));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-结果：{1}", randomString, result.MiniClear()));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-高德地图WebServiceAPI--结束--", randomString));
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public GeoFenceDeleteResult DeleteGeoFencePro(string gid)
        {
            try
            {
                string result = DeleteGeoFence(gid);
                return result.ToObject<GeoFenceDeleteResult>();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public string GetGeoFenceStatus(GeoFenceStatus entity)
        {
            try
            {
                string url = StatusUrl(entity);
                string result = HttpHelper.GetToUrl(url);
                LogsManager.LogPath = string.Empty;
                LogsManager.logFielPrefix = string.Empty;
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-高德地图WebServiceAPI--开结--", randomString));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-IP：{1}", randomString, Net.Ip));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-地址：{1}", randomString, url));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-结果：{1}", randomString, result.MiniClear()));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-高德地图WebServiceAPI--结束--", randomString));
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public GeoFenceStatusResult GetGeoFenceStatusPro(GeoFenceStatus entity)
        {
            try
            {
                string result = GetGeoFenceStatus(entity);
                return result.ToObject<GeoFenceStatusResult>();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

    }
}
